<?php
/**
 * B端erp登录
 */

namespace app\badmin\controller;


use app\badmin\model\CompanyAccount;
use app\common\controller\BadminController;
use think\Exception;
use think\facade\Request;

class Login extends BadminController {

    protected $notNeedToken = ['login'];

    /**
     * 登录
     * @return false|string
     */
    public function login() {
        try {
            if (Request::isPost()) {
                $params = $this->checkParams('phone,password');
                if ($params === false) return $this->resErr();
                $res = CompanyAccount::checkLogin($params);
                if (!$res) {
                    return $this->resErr();
                } else {
                    return $this->resData('登录成功', $res);
                }
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修改密码
     * @return false|string
     */
    public function editPwd() {
        try {
            if (Request::isPost()) {
                $params = $this->checkParams('oldpwd,newPwd,againPwd');
                if ($params === false) return $this->resErr();
                $res = CompanyAccount::editPwd($params);
                if (!$res) return $this->resErr();
                return $this->resData('修改成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    public function quit(){
        try {
            if (Request::isPost()) {
                $params = $this->checkParams('');
                if ($params === false) return $this->resErr();
                $res = CompanyAccount::quit($this->headers,$this->uInfo);
                if (!$res) return $this->resErr();
                return $this->resData('退出成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }


}
